﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;
using C = TrackerRealm.Jazz.Common.JazzConstants;

namespace TrackerRealm.Jazz.Client.Custom
{
    /// <summary>
    /// A '<see cref="jProfile"/>' instance is a collection of roles.
    /// A '<see cref="jProfile"/>' instance is always associated with a single '<see cref="jUser"/>' instance.
    /// <para>A user may have multiple 'profile's</para>
    /// <para>Need a custom jProfile? - then clone, modify and own - </para>
    /// </summary>
    [Jazz]
    [UseEmptyObjectFor(typeof(jProfileBase))]
    public sealed class jProfile : jProfileBase
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly new static jProfile[] EmptyArray = new jProfile[0];
        #region EmptyID
        /// <summary>
        /// The ID for the 'empty' user.
        /// </summary>
        public static readonly Guid EmptyID = new Guid("7FFF00E2-66E8-4F07-A9D8-7CFA3D53BB31");
        #endregion
        #region Factory Methods - Create
        /// <summary>
        /// Create an Empty Object;
        /// </summary>
        /// <returns></returns>
        public static jProfile CreateEmpty(jUser emptyUser)
        {
            return jProfile.Create(C.EMPTY, emptyUser);
        }
        /// <summary>
        /// The created profile is automatically added to the user.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static jProfile Create(string name, jUser user)
        {
            return new jProfile(null, name, user, jRole.EmptyArray);
        }        
        /// <summary>
        /// The created profile is automatically added to the user.
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static jProfile Create(ClientNexus nexus, string name, jUser user)
        {
            return new jProfile(nexus, name, user, jRole.EmptyArray);
        }
        /// <summary>
        /// The created profile is automatically added to the user.
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public static jProfile Create(ClientNexus nexus, string name, jUser user, IEnumerable<jRole> roles)
        {
            return new jProfile(nexus, name, user, roles);
        }
        #endregion
        #region Constructors
        /// <summary>
        /// Constructor
        /// The created profile is automatically added to the user.
        /// </summary>
        /// <param name="nexus">This object will not be 'Bound' to the Client nexus if the value is 'null'.</param>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <param name="roles"></param>
        private jProfile(ClientNexus nexus, string name, jUser user, IEnumerable<jRole> roles)
            : base(nexus, name, user, roles)
        {
        }
        #endregion
        #region User
        /// <summary>
        /// 
        /// </summary>
        public jUser User
        {
            get { return (jUser)base._User; }
        } 
        #endregion
        #region Roles
        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<jRole> Roles
        {
            get { return base._Roles.Cast<jRole>(); }
        } 
        #endregion


        #region Add/Remove roles
        /// <summary>
        /// Add a role to this profile.
        /// </summary>
        /// <param name="role"></param>
        public void AddRole(jRole role)
        {
            base._AddRole(role);
        }
        /// <summary>
        /// Remove a role from this profile.
        /// </summary>
        /// <param name="role"></param>
        public void RemoveRole(jRole role)
        {
            base._RemoveRole(role);
        }
        #endregion
        #region Rename
        /// <summary>
        /// If the name is already in use an '<see cref="ArgumentException"/>' will be thrown.
        /// </summary>
        /// <param name="name">The 'name' is only the 'profile' segment of the 'Profile' name.</param>
        public void Rename(string name)
        {
            this.Name = name;
        }
        #endregion

        #region ACL -Add/Remove
        /// <summary>
        /// The ACL (Access Control List) controls how this '<see cref="jObject"/>' instance
        /// can be accessed.
        /// <para>Note: If the ACL sequence is empty then there are no access restrictions.</para>
        /// </summary>
        public IEnumerable<jAclItem> ACL
        {
            get { return base._ACL; }
        }
        /// <summary>
        /// Add a '<see cref="jAclItem"/>' to this jObject's ACL.
        /// </summary>
        /// <param name="item"></param>
        public void Add(jAclItem item)
        {
            base._Add(item);
        }
        /// <summary>
        /// Remove a '<see cref="jAclItem"/>' from this jObject's ACL.
        /// </summary>
        /// <param name="item"></param>
        public void Remove(jAclItem item)
        {
            base._Remove(item);
        }
        #endregion

        #region Assign Fixed Ids.
        /// <summary>
        /// Assigns a fixed ID to the empty Profile instance.
        /// </summary>
        /// <returns></returns>
        protected internal override Guid OnAssignID()
        {
            if (this.IsEmpty)
                return jProfile.EmptyID;
            else
                return base.OnAssignID();
        }
        #endregion
    }
}
